{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 0: Update the code for Python 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The book was written with Python 2 code. Let's make all the changes needed for Python 3 compatibility at once.\n",
    "\n",
    "We'll also need to update some parts of `network3.py` for Theano 1.0.3.\n",
    "\n",
    "Here's what you need to do:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Option 1: use my files in original-python-3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The only modifications I've made in these files is updating them for Python 3 (specifically, I did the changes below)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Option 2: make all the changes manually"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Do the following replacements (<kbd>Ctrl</kbd> + <kbd>H</kbd> will usually work):"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<table style=\"border: 1px solid black; margin:auto;\">\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <th style=\"border: 1px solid black\">Python 2</th>\n",
    "          <th style=\"border: 1px solid black\">Python 3</th>\n",
    "          <th style=\"border: 1px solid black\">Files</th>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">xrange</td>\n",
    "          <td style=\"border: 1px solid black\">range</td>\n",
    "          <td style=\"border: 1px solid black\">conv.py, network.py, network2.py, network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">print \"whatever\"</td>\n",
    "          <td style=\"border: 1px solid black\">print(\"whatever\")</td>\n",
    "          <td style=\"border: 1px solid black\">mnist_average_darkness.py, mnist_svm.py, network.py, network2.py, network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">cPickle</td>\n",
    "          <td style=\"border: 1px solid black\">pickle</td>\n",
    "          <td style=\"border: 1px solid black\">expand_mnist.py, mnist_loader.py, network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">pickle.load(f)</td>\n",
    "          <td style=\"border: 1px solid black\">pickle.load(f, encoding=\"latin1\")</td>\n",
    "          <td style=\"border: 1px solid black\">expand_mnist.py, mnist_loader.py, network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">return -T.mean(T.log(</td>\n",
    "          <td style=\"border: 1px solid black\">return T.mean(-T.log(</td>\n",
    "          <td style=\"border: 1px solid black\">network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">np.random.RandomState(0).randint(999999)</td>\n",
    "          <td style=\"border: 1px solid black\">np.random.randint(999999)</td>\n",
    "          <td style=\"border: 1px solid black\">network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">iteritems()</td>\n",
    "          <td style=\"border: 1px solid black\">items()</td>\n",
    "          <td style=\"border: 1px solid black\">mnist_average_darkness.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">zip(whatever)</td>\n",
    "          <td style=\"border: 1px solid black\">list(zip(whatever))</td>\n",
    "          <td style=\"border: 1px solid black\">conv.py, mnist_loader.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">from theano.tensor.signal import downsample</td>\n",
    "          <td style=\"border: 1px solid black\">from theano.tensor.signal.pool import pool_2d</td>\n",
    "          <td style=\"border: 1px solid black\">network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">pooled_out = downsample.max_pool_2d(</td>\n",
    "          <td style=\"border: 1px solid black\">pooled_out = pool_2d(</td>\n",
    "          <td style=\"border: 1px solid black\">network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">num_training_batches = size...<br>num_validation_batches = size...<br>num_test_batches = size...</td>\n",
    "          <td style=\"border: 1px solid black\">num_training_batches = int(size...)<br>num_validation_batches = int(size...)<br>num_test_batches = int(size...)</td>\n",
    "          <td style=\"border: 1px solid black\">network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">ds=</td>\n",
    "          <td style=\"border: 1px solid black\">ws=</td>\n",
    "          <td style=\"border: 1px solid black\">network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">from theano.tensor.nnet import conv</td>\n",
    "          <td style=\"border: 1px solid black\">from theano.tensor.nnet import conv2d</td>\n",
    "          <td style=\"border: 1px solid black\">network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">conv_out = conv.conv2d(</td>\n",
    "          <td style=\"border: 1px solid black\">conv_out = conv2d(</td>\n",
    "          <td style=\"border: 1px solid black\">network3.py</td>\n",
    "        </tr>\n",
    "        <tr style=\"border: 1px solid black\">\n",
    "          <td style=\"border: 1px solid black\">image_shape=self.image_shape</td>\n",
    "          <td style=\"border: 1px solid black\">input_shape=self.image_shape</td>\n",
    "          <td style=\"border: 1px solid black\">network3.py</td>\n",
    "        </tr>\n",
    "</table>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
